package extend;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.sql.SQLException;
import java.util.Properties;

public class MyJdbcNiubeTest {

    public static void main(String[] args) throws SQLException {
        Properties properties = new Properties();
        properties.setProperty("jdbc.url","jdbc:mysql://doitedu:3306/doit46");
        properties.setProperty("jdbc.username","root");
        properties.setProperty("jdbc.password","root");

        MyJdbcNiube sink = MyJdbcNiube.niube(
                "insert into user_action_count (user_id,event_id,event_cnt) values (?,?,?) on duplicate key update event_cnt = ? ",
                (statement, json) -> {
                    JSONObject jsonObject = JSON.parseObject(json);

                    statement.setInt(1, jsonObject.getIntValue("user_id"));
                    statement.setString(2, jsonObject.getString("event_id"));
                    statement.setLong(3, jsonObject.getLongValue("event_cnt"));
                    statement.setLong(4, jsonObject.getLongValue("event_cnt"));
                },
                properties
        );


        // {"user_id":1,"event_id":"A","event_cnt":5000}
        sink.emitData("{\"user_id\":1,\"event_id\":\"A\",\"event_cnt\":9000}");
        sink.emitData("{\"user_id\":8,\"event_id\":\"K\",\"event_cnt\":28000}");



    }

}
